/**************************************************************************************
*  Adc.java
*  Program demonstrating the use ADC/DAC converters on LPC214x.
**************************************************************************************
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL RTJ COMPUTING BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* Created by:
*      RTJ Computing
**************************************************************************************/
import lpc2xxx.edu.*;
import lpc2xxx.hw.*;


public class Adc extends Thread
{
    final static int aoutMax = 0x3FF;
    static Switch sw = new Switch();
    static int aout = 0;
    
    public static void main(String[] args) 
    {
        Console.println("");
        Console.println("LPC214x Analog to Digital Conversion");
        Console.println("This program is for use with LPC2148 Education board");
        Console.println("Jumper settings:");
        Console.println("  connect    J15,J18,J22\r\n");
        Console.println("Use joystick left/right to change AOUT value");    
        Console.println("To exit press ESC");
        Console.println("");

        Adc adc = new Adc();
        adc.start();
        
        ADC a1 = new ADC(ADC.A1);
        ADC a2 = new ADC(ADC.A2);
        ADC a3 = new ADC(ADC.A3);

        int milliVolt1 = -1;
        int milliVolt2 = -1;
        int milliVolt3 = -1;

        while (true)
        {
            if (Console.getKey() == Console.ESC)
                break;

            int mv1 = a1.getmV();
            int mv2 = a2.getmV();
            int mv3 = a3.getmV();
            if (mv1 != milliVolt1 || mv2 != milliVolt2 || mv3 != milliVolt3)
            {
                milliVolt1 = mv1;
                milliVolt2 = mv2;
                milliVolt3 = mv3;
                Console.print("\rAIN1: " + Integer.toString(mv1) + "mV");
                Console.print(", AIN2: " + Integer.toString(mv2) + "mV");
                Console.print(", AOUT: " + Integer.toString(mv3) + "mV         ");
                try
                {
                    Thread.sleep(250);
                }
                catch (InterruptedException e) {}
            }

        }
        Console.println("");
    }

    public Adc()
    {
        sw.start();
        setDaemon(true);
    }

    public void run()
    {
        DAC dac = new DAC();
        dac.set(aout, false);
        
        boolean waitSw = false;
        while (true)
        {
            if (waitSw) 
            {
                if (!sw.left() && !sw.right())
                    waitSw = false;
            }
            else
            {
                int ao = aout;
                if (sw.left() && aout > 0)
                    ao -= 0x10;
                else if (sw.right() && aout < aoutMax)
                    ao += 0x10;

                if (ao != aout)
                {
                    waitSw = true;
                    if (ao < 0)
                        aout = 0;
                    else if (ao > aoutMax)
                        aout = aoutMax;
                    else
                        aout = ao;
                    dac.set(aout, false);
                }
            }
            yield();
        }
    }
}

